package com.tsing.cli.common.exception;

import com.tsing.cli.common.vo.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.stream.Collectors;

/**
 * 全局异常处理器
 *
 * @author TheTsing
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResultData<?> exceptionHandler(Exception e) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        try (stringWriter; printWriter) {
            e.printStackTrace(printWriter);
            log.error(stringWriter.toString());
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return ResultData.error(ResponseCode.SERVER_ERROR.getCode(), ResponseCode.SERVER_ERROR.getDescription());
    }

    @ExceptionHandler(CustomException.class)
    public ResultData<?> exceptionHandler(CustomException e) {
        return ResultData.error(e.getCode(), e.getDescription());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultData<?> exceptionHandler(MethodArgumentNotValidException e) {
        return ResultData.error(ResponseCode.METHOD_ARGUMENT_NOT_VALID_EXCEPTION.getCode(), e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining("、")));
    }

    @ExceptionHandler(BindException.class)
    public ResultData<?> exceptionHandler(BindException e) {
        return ResultData.error(ResponseCode.BIND_EXCEPTION.getCode(), e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining("、")));
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResultData<?> exceptionHandler(HttpRequestMethodNotSupportedException e) {
        return ResultData.error(ResponseCode.HTTP_REQUEST_METHOD_NOT_SUPPORTED_EXCEPTION.getCode(), ResponseCode.HTTP_REQUEST_METHOD_NOT_SUPPORTED_EXCEPTION.getDescription());
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResultData<?> exceptionHandler(HttpMessageNotReadableException e) {
        return ResultData.error(ResponseCode.HTTP_MESSAGE_NOT_READABLE_EXCEPTION.getCode(), ResponseCode.HTTP_MESSAGE_NOT_READABLE_EXCEPTION.getDescription());
    }

    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public ResultData<?> exceptionHandler(HttpMediaTypeNotSupportedException e) {
        return ResultData.error(ResponseCode.HTTP_MEDIA_TYPE_NOT_SUPPORTED_EXCEPTION.getCode(), ResponseCode.HTTP_MEDIA_TYPE_NOT_SUPPORTED_EXCEPTION.getDescription());
    }

    @ExceptionHandler(MissingServletRequestPartException.class)
    public ResultData<?> exceptionHandler(MissingServletRequestPartException e) {
        return ResultData.error(ResponseCode.MISSING_SERVLET_REQUEST_PART_EXCEPTION.getCode(), ResponseCode.MISSING_SERVLET_REQUEST_PART_EXCEPTION.getDescription());
    }

    @ExceptionHandler(AccessDeniedException.class)
    public ResultData<?> exceptionHandler(AccessDeniedException e) {
        throw e;
    }

    @ExceptionHandler(BadCredentialsException.class)
    public ResultData<?> exceptionHandler(BadCredentialsException e) {
        return ResultData.error(ResponseCode.BAD_CREDENTIALS_EXCEPTION.getCode(), e.getMessage());
    }

    @ExceptionHandler(DisabledException.class)
    public ResultData<?> exceptionHandler(DisabledException e) {
        return ResultData.error(ResponseCode.DISABLED_EXCEPTION.getCode(), ResponseCode.DISABLED_EXCEPTION.getDescription());
    }

}
